package com.zengdb.jdbc.service.impl;

import java.sql.SQLException;
import java.util.List;

import com.zengdb.jdbc.dao.IStudentDAO;
import com.zengdb.jdbc.dao.impl.StudentDAOImpl;
import com.zengdb.jdbc.domain.Student;
import com.zengdb.jdbc.service.IStudentService;
import com.zengdb.jdbc.util.JdbcUtil;

public class StudentServiceImpl implements IStudentService {

	IStudentDAO studentDao = new StudentDAOImpl();
	
	@Override
	public void save(Student stu) {
		try {
			//开启事务
			JdbcUtil.beginTransaction();
			
			//调用DAO层操作业务
			studentDao.save(stu);
			
			//提交事务
			JdbcUtil.commitTransaction();
		} catch (SQLException e) {
			try {
				//回滚事务
				JdbcUtil.rollbackTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	@Override
	public void delete(Long id) {
		try {
			//开启事务
			JdbcUtil.beginTransaction();
			
			//调用DAO层操作业务
			studentDao.delete(id);
			
			//提交事务
			JdbcUtil.commitTransaction();
		} catch (SQLException e) {
			try {
				//回滚事务
				JdbcUtil.rollbackTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
				throw new RuntimeException(e);
			}
		}

	}

	@Override
	public void modify(Student stu) {
		try {
			//开启事务
			JdbcUtil.beginTransaction();
			
			//调用DAO层操作业务
			studentDao.modify(stu);
			
			//提交事务
			JdbcUtil.commitTransaction();
		} catch (SQLException e) {
			try {
				//回滚事务
				JdbcUtil.rollbackTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	@Override
	public Student queryOne(Long id) {

		return studentDao.queryOne(id);
	}

	@Override
	public List<Student> queryList() {
		return studentDao.queryList();
	}

}
